package com.itboye.ihomebank.activity.home;

import android.os.Bundle;
import android.view.View;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;

import com.itboye.ihomebank.R;
import com.itboye.ihomebank.adapter.WodeFangyYuanAdapter02;
import com.itboye.ihomebank.base.BaseOtherActivity;
import com.itboye.ihomebank.bean.ResultEntity;
import com.itboye.ihomebank.bean.WoDeFangYuanBean;
import com.itboye.ihomebank.constants.SPContants;
import com.itboye.ihomebank.custom.XListView;
import com.itboye.ihomebank.presenter.HousePresenter;
import com.itboye.ihomebank.util.SPUtils;
import com.scwang.smartrefresh.header.MaterialHeader;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.listener.OnRefreshListener;

import java.util.ArrayList;
import java.util.List;
import java.util.Observable;
import java.util.Observer;

/**
 * 我的租房(签约申请房源列表)
 */
public class ActivityWoDeQiuZu extends BaseOtherActivity implements Observer {
	View v_statusbar;
	SmartRefreshLayout refresh;
	private ListView xlistView;
	private HousePresenter housePresenter;
	private String uid;
	private WodeFangyYuanAdapter02 adapter;
	private ImageView close_icon;
	TextView imgEmpty;
	private TextView add_shap_title_tv;
	private int pageNo = 1;

	List<WoDeFangYuanBean> woDeFangYuanBeans;
	@Override
	public void onClick(View v) {
		switch (v.getId()) {
			case R.id.close_icon:
				finish();
				break;
			default:
				break;
		}
	}

	@Override
	protected int layout_id() {
		return R.layout.activity_wodefangyuan;
	}

	@Override
	protected void onInitController() {
	}

	@Override
	protected void onInit() {
		setStatusBarHeight(v_statusbar);
		add_shap_title_tv.setText("我的求租");
		showProgressDialog("数据加载中,请稍后", true);
		uid = SPUtils.get(this, null, SPContants.USER_ID, "") + "";

		woDeFangYuanBeans=new ArrayList<>();
		housePresenter = new HousePresenter(this);

		caozuo();
		adapter = new WodeFangyYuanAdapter02(this, woDeFangYuanBeans,
				R.layout.item_wodefangyuan);
		xlistView.setAdapter(adapter);
	}
	private void caozuo(){
		refresh = (SmartRefreshLayout) findViewById(R.id.refresh);
		refresh.setEnableAutoLoadmore(true);//开启自动加载功能（非必须）

		//触发自动刷新
		refresh.autoRefresh();
		refresh.setEnableAutoLoadmore(false);//禁止加载更多
		refresh.setOnRefreshListener(new OnRefreshListener() {
			@Override
			public void onRefresh(RefreshLayout refreshlayout) {
				refreshlayout.getLayout().postDelayed(new Runnable() {
					@Override
					public void run() {
						pageNo = 1;
						housePresenter.getMyRentHouse(uid);
					}
				}, 0);
			}
		});
		refresh.setRefreshHeader(new MaterialHeader(this).setShowBezierWave(true));
		refresh.finishLoadmore();
	}
	@Override
	protected void onReadIntent(Bundle bundle) {
	}

	@SuppressWarnings("unchecked")
	@Override
	public void update(Observable observable, Object data) {
		ResultEntity entity = handlerError(data);
		if (entity != null) {
			if (entity.getEventType() == HousePresenter.woDeFangYuan_success) {
				List<WoDeFangYuanBean> bean = (List<WoDeFangYuanBean>) entity
						.getData();
				if (pageNo == 1) {//下拉刷新
					woDeFangYuanBeans.clear();
					adapter.notifyDataSetChanged();
					//结束加载
					refresh.finishRefresh();
				} else {//上拉加载
					adapter.notifyDataSetChanged();
					//结束加载
					refresh.finishLoadmore();
				}
				if (bean != null) {
					if (bean.size() <= 0) {
						imgEmpty.setVisibility(View.VISIBLE);
					} else {
						woDeFangYuanBeans.addAll(bean);
						imgEmpty.setVisibility(View.GONE);
					}
				}
				if (bean.size() <= 0) {
					imgEmpty.setVisibility(View.VISIBLE);
				}

			} else if (entity.getEventType() == HousePresenter.woDeFangYuan_fail) {
				refresh.finishLoadmore();
				refresh.finishRefresh();
			}
		}
		try {
			closeProgressDialog();
		} catch (Exception e) {
			// TODO: handle exception
		}
	}

}
